package com.axc.gallery2.local;

import java.io.IOException;

import android.app.WallpaperManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.util.DisplayMetrics;
import android.view.Display;

public class SetScale extends Thread
{
	private WallpaperManager wallpaperManager;
	private Context context;
	private Bitmap bm;
	private Display display;

	public SetScale(Context context,Bitmap bitmap,Display display)
	{
		this.context=context;
		this.bm= bitmap;
		this.display=display;
	}



	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
		try {

			wallpaperManager = WallpaperManager.getInstance(context);
			wallpaperManager.setBitmap(getScaleImg(bm));	
		} catch (IOException e) {
			String msg = "设置桌面背景发生异常:" + e.getLocalizedMessage();
			throw new ExceptionInInitializerError(msg);
		}
	}

	private  Bitmap getScaleImg(Bitmap bm) {

		DisplayMetrics dm = new DisplayMetrics();
		display.getMetrics(dm);
		// 获得图片的宽高
		int width = bm.getWidth();
		int height = bm.getHeight();
		int dw= dm.widthPixels;
		int dh = dm.heightPixels;
		// 计算缩放比例
		float scaleWidth = ((float) dw)  / width;
		float scaleHeight = ((float)dh) /  height;
		// 取得想要缩放的matrix参数
		Matrix matrix = new Matrix();
		matrix.postScale(scaleWidth, scaleHeight);
		// 得到新的图片
		Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
				true);
		return newbm;

	}
}

